package org.apache.servicecomb.fence.resource.userHair;

import org.apache.ibatis.annotations.*;
import org.apache.servicecomb.fence.api.resource.UserHair;

import java.util.List;

@Mapper
public interface UserHairMapper {
    @Insert("INSERT INTO t_user_hair (user_name, hair_count, hair_color, hair_type) VALUES (#{userName}, #{hairCount}, #{hairColor}, #{hairType})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(UserHair userHair);

    @Update("UPDATE t_user_hair SET user_name = #{userName}, hair_count = #{hairCount}, hair_color = #{hairColor}, hair_type = #{hairType} WHERE id = #{id}")
    int update(UserHair userHair);

    @Delete("DELETE FROM t_user_hair WHERE id = #{id}")
    int deleteById(int id);

    @Select("SELECT * FROM t_user_hair WHERE id = #{id}")
    UserHair getById(int id);

    @Select("SELECT * FROM t_user_hair")
    List<UserHair> getAll();

    @Select("""
    <script>
    SELECT * FROM t_user_hair WHERE 1=1
    <if test="hairColor != null and hairColor != ''"> AND hair_color = #{hairColor}</if>
    <if test="hairType != null and hairType != ''"> AND hair_type = #{hairType}</if>
    <if test="minHairCount != null"> AND hair_count >= #{minHairCount}</if>
    </script>""")
    List<UserHair> search(@Param("hairColor") String hairColor,
                          @Param("hairType") String hairType,
                          @Param("minHairCount") Long minHairCount);

    @Select("SELECT * FROM t_user_hair WHERE user_name = #{userName}")
    UserHair getByUserName(String userName);

    @Update("UPDATE t_user_hair SET hair_count = #{hairCount} WHERE id = #{id}")
    int updateHairCount(@Param("id") int id, @Param("hairCount") long hairCount);

    @Update("UPDATE t_user_hair SET hair_color = #{hairColor} WHERE id = #{id}")
    int updateHairColor(@Param("id") int id, @Param("hairColor") String hairColor);

    @Update("UPDATE t_user_hair SET hair_type = #{hairType} WHERE id = #{id}")
    int updateHairType(@Param("id") int id, @Param("hairType") String hairType);
}